package com.itheima.dao.imp;

import com.itheima.damain.Student;
import com.itheima.dao.lnter.IStudentDao;

import java.util.ArrayList;

public class StudentDaolmpl implements IStudentDao {
    private static ArrayList<Student> list = new ArrayList<>();

    static {
        Student wu = new Student("0001", "吴亦凡", 31, "1991-01-01");
        Student lu = new Student("0002", "鹿晗", 31, "1991-01-01");
        Student huang = new Student("0003", "黄子韬", 31, "1991-01-01");
        Student zhang = new Student("0004", "张艺兴", 31, "1991-01-01");
        list.add(wu);
        list.add(lu);
        list.add(huang);
        list.add(zhang);
    }

    //添加学生
    @Override
    public void addStudent(Student student) {
        list.add(student);
    }

    // 删除学生
    @Override
    public void deleteStudent(String sid) {
        int index = getindex(sid);

        list.remove(index);

    }

    //查看学生
    @Override
    public ArrayList<Student> findAllStudent() {
        return list;
    }

    // 修改学生
    @Override
    public void updateStudent(String sid, Student student) {
        int index = getindex(sid);
        list.set(index, student);

    }


    // 根据学号判断索引
    @Override
    public int getindex(String sid) {

        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if (s != null && s.getSid().equals(sid)) {
                return i;
            }
        }
        return -1;
    }
}
